<?php

class ReviewMapper extends AbstractMapper
{

    protected function _init()
    {
	$this->setModel(new ReviewModel());
	$this->setTableName(REVIEW_TABLE);
    }

    public function getReviewList($params = array())
    {
	return $this->getAllWithRelation(array_merge(array(
			    'select' => array('r.*'),
			    'from' => 'review AS r',
			    'join' => 'INNER JOIN user ON user.id=r.user_id LEFT JOIN category ON category.id = r.category_id',
			    'order' => 'add_date DESC',
				), $params), array('user', 'category'));
    }

    public function getByUserId($id, $page = 1, $limit = 10)
    {
	if (!$id) {
	    return false;
	}
	return $this->getAllWithRelation(array(
		    'select' => array('r.*'),
		    'from' => 'review AS r',
		    'join' => 'LEFT JOIN category ON category.id = r.category_id',
		    'order' => 'add_date DESC',
		    'where' => 'r.user_id = :user_id',
		    'limit' => $limit,
		    'offset' => ($page - 1) * $limit,
		    'params' => array(':user_id' => $id)
			), array('category'));
    }

    public function getSimilarModels($id, $text)
    {
	if (!$text) {
	    return false;
	}
	return $this->getAllWithRelation(array(
		    'select' => array('r.*'),
		    'from' => 'review AS r',
		    'join' => 'INNER JOIN user ON user.id = r.user_id',		
		    'where' => "MATCH (r.title) AGAINST ('" . $text . "') AND r.id <> '" . $id . "'",
		    'limit' => '5',		    		     
			), array('user'));
    }

}
